package by.grsu.kononovich.page.simple;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.AdministratorDao;
import by.grsu.kononovich.entity.Administrator;


public class AdministratorEditPage extends WebPage {
	public AdministratorEditPage(final Administrator admin){
		super();
		
		final Form<Administrator> form = new Form<Administrator>("form", new CompoundPropertyModel<Administrator>(admin));
		add(form);
		
		form.add(new TextField<String>("Name"));
		form.add(new TextField<String>("Surname"));
		
		form.add(new SubmitLink("Save"){
			@Override
			public void onSubmit(){
				super.onSubmit();
				
				final AdministratorDao adminDao = new AdministratorDao(MyFirstWebApplication.DATA_XML_FOLDER);
				
				if (admin.getId() == null){
					adminDao.saveNew(admin);
				} else {
					adminDao.update(admin);
				}
				
				setResponsePage(new AdministratorsListPage());
				System.out.println("button Save pressed");
			}
		});
		
		form.add(new Link("Cancel"){
			@Override
			public void onClick(){
				setResponsePage(new AdministratorsListPage());
				System.out.println("button Cancel pressed");
			}
		});
	}
}
